const {
  baseUrl, baseUrl2
} = require("../config/config")

function initFun() {
  //日期格式化函数
  Date.prototype.format = function (formatStr) {
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];
    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
    str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    return str;
  }
}
// Class Http 
class Http{
   async getToken(){
    const code= await new Promise((resolve,reject)=>{
      wx.login({
        complete: (res) => {
          resolve(res.code)
        },
      })
    });
    console.log(code)
    const token =await new Promise((resolve,reject)=>{
      wx.request({
        url: baseUrl+'/code2openid',
        method:"POST",
        data:{
          code
        },
        success:res=>{
          resolve(res.data.token)
        }
      })
    });
   this.token=token;
   }
   async get (paramsObj, isShowError) {
    // content-type 默认为 application/json
    if(!this.token){
      await this.getToken()
    }
    return new Promise((resolve, reject) => {
      isShowError = isShowError || false;
      paramsObj.method="GET"
      paramsObj.header={
        token:this.token
      }
     
      paramsObj.url=baseUrl + paramsObj.url;
      paramsObj.success = res => {
        
         if (isShowError&&res.statusCode!=200) {
          wx.showModal({
            title: '请求错误',
            content: res.data.message
          })
        }
        resolve(res)
      };
      paramsObj.fail = err => {
        if (isShowError) {
          wx.showModal({
            title: '请求错误',
            content: paramsObj.url
          })
        }
        reject(err)
      };
      wx.request(paramsObj)
    })
  };
   async post (paramsObj, isShowError) {
     // content-type 默认为 application/json
     if(!this.token){
      await this.getToken()
    }
    return new Promise((resolve, reject) => {
      isShowError = isShowError || false;
      paramsObj.method="POST"
      paramsObj.header={
        token:this.token
      }
      paramsObj.url=baseUrl + paramsObj.url;
      paramsObj.success = res => {
        // console.log(res)
        if (isShowError&&res.statusCode!=200) {
          wx.showModal({
            title: '请求错误',
            content: res.data.message
          })
        }
        resolve(res)
      };
      paramsObj.fail = err => {
        if (isShowError) {
          wx.showModal({
            title: '请求错误',
            content: paramsObj.url
          })
        }
        reject(err)
      };
      wx.request(paramsObj)
    })
  };
  async get2(paramsObj, isShowError) {
    // content-type 默认为 application/json
    if (!this.token) {
      await this.getToken()
    }
    return new Promise((resolve, reject) => {
      isShowError = isShowError || false;
      paramsObj.method = "GET"
      paramsObj.header = {
        token: this.token
      }

      paramsObj.url = baseUrl2 + paramsObj.url;
      paramsObj.success = res => {

        if (isShowError && res.statusCode != 200) {
          wx.showModal({
            title: '请求错误',
            content: res.data.message
          })
        }
        resolve(res)
      };
      paramsObj.fail = err => {
        if (isShowError) {
          wx.showModal({
            title: '请求错误',
            content: paramsObj.url
          })
        }
        reject(err)
      };
      wx.request(paramsObj)
    })
  };
  async post2(paramsObj, isShowError) {
    // content-type 默认为 application/json
    if (!this.token) {
      await this.getToken()
    }
    return new Promise((resolve, reject) => {
      isShowError = isShowError || false;
      paramsObj.method = "POST"
      paramsObj.header = {
        token: this.token
      }
      paramsObj.url = baseUrl2 + paramsObj.url;
      paramsObj.success = res => {
        // console.log(res)
        if (isShowError && res.statusCode != 200) {
          wx.showModal({
            title: '请求错误',
            content: res.data.message
          })
        }
        resolve(res)
      };
      paramsObj.fail = err => {
        if (isShowError) {
          wx.showModal({
            title: '请求错误',
            content: paramsObj.url
          })
        }
        reject(err)
      };
      wx.request(paramsObj)
    })
  };
}
module.exports = {
  initFun,
  Http
}
// ---------------Http api---------------
/*
(new Http()).$get({
  url:'/getMyname'
}).then(res=>{
  console.log(res)
}).catch(err=>{
  throw new Error()
})
*/
//---------------------------------------
